/*
* @(#)StepInformationWindow.java
*
* Copyright (c) 2004 DCIVision Ltd
* All rights reserved.
*
* This software is the confidential and proprietary information of DCIVision
* Ltd ("Confidential Information").  You shall not disclose such Confidential
* Information and shall use it only in accordance with the terms of the license
* agreement you entered into with DCIVision Ltd.
*/

package com.dcivision.workflow.applet.editor;

/**
  WorkflowErrorConstant.java

  This class is to store all error code constants which used in systems.

    @author          Zealot
    @company         DCIVision Limited
    @creation date   1/13/2005
    @version         $Revision: 1.4 $
*/

import java.awt.Frame;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.io.ObjectInputStream;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;

import org.jgraph.graph.DefaultEdge;
import org.jgraph.pad.GPUserObject;

import com.dcivision.workflow.applet.AppletResource;
import com.dcivision.workflow.applet.ListObject;
import com.dcivision.workflow.bean.WorkflowStep;

public class RouteWindow extends JDialog {
  
  public static final String REVISION = "$Revision: 1.4 $";
  JPanel panel1 = new JPanel();
  JLabel jLabel1 = new JLabel();
  JComboBox jCBRoute = new JComboBox();
  JButton jButton1 = new JButton();
  DefaultEdge edge = null;
  String[] routeLable = null;
  String actionType = null;
  Editor editor=null;
  public RouteWindow(Frame frame, String title, boolean modal) {
    super(frame, title, modal);
    try {
      jbInit();
      pack();
    }
    catch(Exception ex) {
      ex.printStackTrace();
    }
  }

  public RouteWindow() {
    this(null, "", false);
  }
  private void jbInit() throws Exception {
    panel1.setLayout(null);
    jLabel1.setText(AppletResource.lableArr[161]);
    jLabel1.setBounds(new Rectangle(10, 45, 125, 20));
    jCBRoute.setBounds(new Rectangle(143, 42, 151, 22));
    jButton1.setBounds(new Rectangle(117, 93, 84, 25));
    jButton1.setText(AppletResource.lableArr[24]);
    jButton1.addActionListener(new RouteWindow_jButton1_actionAdapter(this));
    getContentPane().add(panel1);
    panel1.add(jLabel1, null);
    panel1.add(jCBRoute, null);
    panel1.add(jButton1, null);
  }
  
  public boolean initData(){
    ListObject[] listObjectArr = new ListObject[2];
    listObjectArr[0]=new ListObject();
    listObjectArr[1]=new ListObject();
    if(actionType.equals(WorkflowStep.ACTION_TYPE_APPROVAL)){
      listObjectArr[0].setID(WorkflowStep.PARENT_TYPE_APPROVED);
      listObjectArr[0].setName(AppletResource.lableArr[87]);
      listObjectArr[1].setID(WorkflowStep.PARENT_TYPE_APPROVED);
      listObjectArr[1].setName(AppletResource.lableArr[88]);
      jCBRoute.addItem(listObjectArr[0]);
      jCBRoute.addItem(listObjectArr[1]);
    }else if(actionType.equals(WorkflowStep.ACTION_TYPE_BOOLEAN)){
      
      
      listObjectArr[0].setID(WorkflowStep.PARENT_TYPE_YES);
      listObjectArr[0].setName(AppletResource.lableArr[90]);
      listObjectArr[1].setID(WorkflowStep.PARENT_TYPE_NO);
      listObjectArr[1].setName(AppletResource.lableArr[89]);
      jCBRoute.addItem(listObjectArr[0]);
      jCBRoute.addItem(listObjectArr[1]);
    }else if(actionType.substring(0,1).equals(WorkflowStep.ACTION_TYPE_SYSTEM_AUTO)){
      String actionID=actionType.substring(1);
      String getDataURL  = "/workflow/appletMiddleware?dataType=WORKFLOW_ACTION&ACTIONID="+actionID;
      ObjectInputStream inputFromServlet = null;
      try{
        inputFromServlet = editor.getHttpOperation().getObjectInputStreamFromServlet(getDataURL);
        if(inputFromServlet == null){
          return false;
        }    
        Object obj = null;
        //obj = inputFromServlet.readObject();
      //  get bind list for select
        obj = inputFromServlet.readObject();
        if (obj instanceof String[]) {
          routeLable = (String[]) obj;
        } else if (obj instanceof Exception) {
          //otherwise type cast it to Exception and throw that exception.
          throw (Exception) obj;
        }   
      }catch(Exception e){
        e.printStackTrace();
        return false;
      }
      for(int i=0;i<routeLable.length;i++){
        ListObject listObject = new ListObject();
        listObject.setID("S"+i);
        listObject.setName(routeLable[i]);
        jCBRoute.addItem(listObject);
        
      }
    }
    return true;
  }
  public DefaultEdge getEdge(){
    return this.edge;    
  }
  
  public void setEdge(DefaultEdge edge){
    this.edge = edge;    
  }
  
  public void setEditor(Editor editor){
    this.editor = editor;    
  }
  
  public void setRouteLable(String[] routeLable){
    this.routeLable = routeLable;
  }
  
  public void setActionType(String actionType){
    this.actionType = actionType;
  }
  
  void jButton1_actionPerformed(ActionEvent e) {
    int routeNum = jCBRoute.getItemCount();
    ListObject listObject=((ListObject)jCBRoute.getSelectedItem());
    String type;
    if(routeNum>1){
      type=WorkflowStep.PARENT_TYPE_SINGLE+listObject.getID();
    } else {
      type=WorkflowStep.PARENT_TYPE_SINGLE;
    }
    String routeName = listObject.getName();
    //this.edge.setUserObject(routeName);
    GPUserObject connectType = new GPUserObject();
    connectType.putProperty("TYPE",listObject.getID());
    connectType.setValue(routeName);
    this.edge.setUserObject(connectType);
    this.dispose();
  }
}

class RouteWindow_jButton1_actionAdapter implements java.awt.event.ActionListener {
  RouteWindow adaptee;

  RouteWindow_jButton1_actionAdapter(RouteWindow adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.jButton1_actionPerformed(e);
  }
}